<template>
  <el-card class="layout-query">
    <el-form ref="queryRef" :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
      <el-form-item prop="username">
        <el-input v-model="state.queryForm.username" placeholder="根据用户名查询"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button icon="RefreshRight" @click="reset(queryRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <el-card>
    <el-space>
      <el-space>
        <el-button icon="Plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-space>
      <el-space>
        <el-button icon="Delete" plain type="danger" @click="deleteBatchHandle()">批量删除</el-button>
      </el-space>
    </el-space>
    <el-table
      v-loading="state.dataListLoading"
      show-overflow-tooltip
      :data="state.dataList"
      border
      class="layout-table"
      max-height="calc(100vh - var(--theme-header-height) - 40px - 225px)"
      @selection-change="selectionChangeHandle"
    >
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column type="index" label="序号" align="center" width="70" />
      <el-table-column prop="username" label="用户名" align="center" />
      <el-table-column prop="nickname" label="昵称" align="center" />
      <el-table-column prop="avatarUrl" label="头像" align="center">
        <template #default="{ row }">
          <el-image 
            :src="row.avatarUrl" 
            fit="fill" 
            style="width: 80px; height: 80px"
            />
        </template>
      </el-table-column>
      <el-table-column prop="role" label="角色" align="center">
        <template #default="{ row }">
          {{ roleMap.get(row.role?.toString()) || '未知' }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center">
        <template #default="{ row }">
          {{ statusMap.get(row.status?.toString()) || '未知' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="200">
        <template #default="{ row }">
          <el-button type="primary" link @click="addOrUpdateHandle(row as ListVO)">查看</el-button>
          <el-button type="warning" link @click="resetPasswordHandle((row as ListVO).id)">重置密码</el-button>
          <el-button type="danger" link @click="deleteBatchHandle((row as ListVO).id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="state.current"
      :page-sizes="state.pageSizes"
      :page-size="state.pageSize"
      :total="state.total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
    />

    <!-- 弹窗, 新增 / 修改 -->
    <AddOrUpdate 
      ref="addOrUpdateRef" 
      @refresh-data-list="getDataList"
      :statusMap="statusMap"
      :roleMap="roleMap"
      />
  </el-card>
</template>

<script setup lang="ts" name="SysTagIndex">
import { reactive, ref, computed } from 'vue'

import { useCrud } from '@/hooks/useCrud'
import { useDict } from '@/hooks/useDict'

import AddOrUpdate from './components/AddOrUpdate.vue'

import { IHooksOptions } from '@/hooks/useCrud/data.d'

import { ListVO, SearchDTO } from './data'
import { DictHooksOptions } from '@/hooks/useDict/data'

import { resetPasswordHandle } from './api'
import { DictTypes } from './data'

// 获取用户列表
const state: IHooksOptions<SearchDTO, ListVO> = reactive({
  dataListUrl: '/codeDuckUser/list',
  deleteUrl: '/codeDuckUser/removeBatchByIds',
  primaryKey: 'id',
  queryForm: {
    username: '',
  },
})
console.log(state)

const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle, reset } =
  useCrud(state)

const queryRef = ref()
const addOrUpdateRef = ref()
const addOrUpdateHandle = (row?: ListVO) => {
  addOrUpdateRef.value.init(row)
}

const dictState: DictHooksOptions<DictTypes> = reactive({
  dictTypeList: ['user_status', 'user_role'],
})
const { getDictList } = useDict(dictState)

// 状态映射
const statusMap = computed(() => {
  const map = new Map<string, string>()
  // 使用可选链操作符和空值合并操作符
  const statusDict = dictState.dictMap?.['user_status'] ?? []
  statusDict.forEach((item) => {
    if (item.itemValue && item.itemLabel) {
      map.set(item.itemValue, item.itemLabel)
    }
  })
  return map
})

// 角色映射
const roleMap = computed(() => {
  const map = new Map<string, string>()
  const roleDict = dictState.dictMap?.['user_role'] ?? []
  roleDict.forEach((item) => {
    if (item.itemValue && item.itemLabel) {
      map.set(item.itemValue, item.itemLabel)
    }
  })
  return map
})
</script>
<style scoped>
.layout-table :deep(.el-table__body-wrapper .el-checkbox) {
  display: flex;
  justify-content: center;
  width: 100%;
}
</style>